package com.asia5b.a5mall.member.activity

import android.os.Bundle
import androidx.fragment.app.DialogFragment
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.asia5b.a5bmall.resource.ActCons
import com.asia5b.a5mall.member.R
import com.asia5b.a5mall.member.adapter.FootAdapter
import com.asia5b.a5mall.member.databinding.ActivityFootBinding
import com.asia5b.a5mall.member.utils.FootAdapterListener
import com.asia5b.a5mall.middleware.base.SimpleActivity
import com.asia5b.a5mall.middleware.logic.viewmodel.member.FootActivityVM
import com.asia5b.a5mall.middleware.model.base.ResultModel
import com.asia5b.a5mall.middleware.model.member.FootModel
import com.asia5b.a5mall.middleware.model.shop.AbsGoodsModel
import com.asia5b.a5mall.middleware.utils.judge
import com.asia5b.a5mall.middleware.utils.newEmptyView
import com.asia5b.a5mall.middleware.utils.showMessage
import com.asia5b.a5mall.middleware.widget.CancelSureViewModel
import com.scwang.smartrefresh.layout.api.RefreshLayout
import kotlinx.android.synthetic.main.activity_foot.*

/**
 * Created by zhangyuncai on 2019/12/23.
 * 足迹
 */
@Route(path = ActCons.FootActivity)
class FootActivity : SimpleActivity<FootActivityVM, ActivityFootBinding>() {
    override val viewModelClazz: Class<FootActivityVM>
        get() = FootActivityVM::class.java

    override fun getDataBinding(): ActivityFootBinding {
        return ActivityFootBinding.inflate(layoutInflater)
    }

    override var sIsDark = true

    val mDatas = arrayListOf<FootModel>()
    val mAdapter by lazy { FootAdapter(mDatas, litener) }
    val mRecyclerView by lazy { rv_foot }
    var isShowDelete = false

    val emptyView by lazy { newEmptyView(mActivity) }

    val litener = object : FootAdapterListener {
        override fun checkedChange() {
            setCheckAllView()
        }

        override fun deleteItem(item: AbsGoodsModel) {
            CancelSureViewModel(mActivity, R.string.是否需要删除选中商品) {
                mViewModel.deleteFoots(arrayListOf<Int>(item.absId))
            }.show()
        }

        override fun addCart(item: AbsGoodsModel) {
            val bundel = Bundle()
            bundel.putBoolean("type", true)
            bundel.putSerializable("data", item)
            bundel.putInt("goodsId", item.absGoodsId)
            val dialog = ARouter.getInstance().build(ActCons.GoodsSpecDF).with(bundel).navigation()
            if (dialog != null && dialog is DialogFragment) {
                dialog.show(supportFragmentManager, "GoodsSpecDF")
            }
        }

    }

    override fun initView() {
        mSmartRefreshLayout = mDataBinding.autoFoot
        mDataBinding.autoFoot.setEnableRefresh(false)
        setEditStateView()
        mRecyclerView.run {
            layoutManager = LinearLayoutManager(mActivity)
            adapter = mAdapter
        }

        getDatas(0)
    }

    fun setEditStateView() {
        head_foot.setRithtText((isShowDelete).judge(R.string.完成, R.string.编辑))
        mDataBinding.isShowDelete = isShowDelete
        mAdapter.isShowDelete = isShowDelete
        mAdapter.notifyDataSetChanged()
    }

    fun setCheckAllView() {
        var checkAll = true
        for (footModel in mDatas) {
            if (!footModel.history.isNullOrEmpty()) {
                for (footGoodsModel in footModel.history!!) {
                    if (checkAll) {
                        checkAll = footGoodsModel.absGoodsCheckedOb.get()
                    }
                    if (!checkAll) {
                        break
                    }
                }
            }

            if (!checkAll) {
                break
            }
        }
        cb_foot_checkall.isChecked = checkAll
        if (mDatas.size == 0) {
            cb_foot_checkall.isChecked = false
        }

    }

    override fun initEvent() {

        head_foot.setRightClickListener {
            isShowDelete = !isShowDelete
            setEditStateView()
        }

        cb_foot_checkall.setOnClickListener {
            for (footModel in mDatas) {
                footModel.checkOB.set(cb_foot_checkall.isChecked)
                if (!footModel.history.isNullOrEmpty()) {
                    for (footGoodsModel in footModel.history!!) {
                        footGoodsModel.absGoodsCheckedOb.set(cb_foot_checkall.isChecked)
                    }
                }

            }
            setCheckAllView()
            mAdapter.notifyDataSetChanged()
        }

        bt_foot_delete.setOnClickListener {
            val list = arrayListOf<Int>()
            for (footModel in mDatas) {
                if (!footModel.history.isNullOrEmpty()) {
                    for (footGoodsModel in footModel.history!!) {
                        if (footGoodsModel.absGoodsCheckedOb.get()) {
                            list.add(footGoodsModel.absId)
                        }
                    }
                }

            }
            if (list.size == 0) {
                showMessage(getString(R.string.尚未选择商品))
            } else {
                CancelSureViewModel(mActivity, R.string.是否需要删除选中商品) {
                    mViewModel.deleteFoots(list)
                }.show()
            }
        }


    }

  override  fun startObserver()
    {
        mViewModel.deleteFootsResultLiveData.observe(this, Observer {
            deleteFootsResult(it)
        })
        mViewModel.getDatasResultLiveData.observe(this, Observer {
            getDatasResult(it)
        })
    }

    override fun initData() {

    }

    fun getDatas(refresh: Int) {
        mViewModel.getDatas(refresh)

    }

    fun getDatasResult(resultModel: ResultModel<ArrayList<FootModel>>) {
        if (emptyView.parent == null) {
            mAdapter.setEmptyView(emptyView)
        }
        if (resultModel.page == 1) {
            mDatas.clear()
        }
        if (!resultModel.data.isNullOrEmpty()) {
            mAdapter.addData(resultModel.data!!)
        }
        mDataBinding.autoFoot.setEnableLoadMore(resultModel.page < resultModel.sumPage)
    }

    fun deleteFootsResult(ids: ArrayList<Int>) {
        var iterator = mDatas.iterator()
        while (iterator.hasNext()) {
            val model = iterator.next()
            if (!model.history.isNullOrEmpty()) {
                val iterator1 = model.history!!.iterator()
                while (iterator1.hasNext()) {
                    val footGoodsModel = iterator1.next()
                    if (ids.contains(footGoodsModel.absId)) {
                        iterator1.remove()
                    }
                }
            }

        }
        iterator = mDatas.iterator()
        while (iterator.hasNext()) {
            val model = iterator.next()
            if (model?.history?.size == 0) {
                iterator.remove()
            }
        }
        mAdapter.notifyDataSetChanged()
        setCheckAllView()
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        getDatas(2)
    }

}